package com.imau.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.imau.bean.User;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 添加教师
     *
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping
    protected ResultEntity add(@RequestBody User user) {

        //缺少信息


        return  ResultEntity.writeResult( userService.save(user));
    }

    /**
     * 删除
     *
     * @throws ServletException
     * @throws IOException
     */
    @DeleteMapping("/{id}")
    protected ResultEntity removeById(@PathVariable String id) {



        //删除
        boolean b = userService.removeById(id);
        //4.返回响应

        return  ResultEntity.writeResult(b);


    }


    @PutMapping
    protected ResultEntity edit(@RequestBody User user) {

        //3.保存
        boolean b = userService.editUser(user);
        //4.返回响应
        return  ResultEntity.writeResult(b);

    }


    /**
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping
    protected ResultEntity list(HttpServletRequest request,User user) {


        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");

        //3.保存
        IPage<User> page = userService.queryUserList(user,Integer.valueOf(currentPage),Integer.valueOf(pageSize));


        return new ResultEntity(ResultCode.SUCCESS, page);
    }

    @GetMapping("/{id}")
    protected ResultEntity getInfo(@PathVariable String id)  {

        return new ResultEntity(ResultCode.SUCCESS, userService.findById(id));
    }

    /**
     * 分配角色
     *
     * @throws ServletException
     * @throws IOException
     */

    protected ResultEntity assignRoles(Map<String,Object> map) throws ServletException, IOException {

        //1.用户的Id主键

        String id = (String) map.get("id");

        //2.获取角色的集合
       String rids[] = (String[]) map.get("rids");

        //3. 分配角色
         boolean b= userService.assignRoles(id, rids);


        //4.返回响应

           return ResultEntity.writeResult(b);


    }

    /**
     * 登录请求
     * @throws ServletException
     * @throws IOException
     */
    protected void login( )   {

//
//        //前端JSON对象 用户名和密码 现在约定是手机号登录
//        String jsonString = request.getReader().readLine();
//
//        //转成对象
//        User user = JSON.parseObject(jsonString, User.class);
//        //1.先按照手机号或是登录账号查询用户
//        User user2 = userService.findByField(new User(user.getMobile()));
//        //如果用户不存在或密码错误，反馈前端信息
//        if (user2 == null || !user2.getPassword().equals(user.getPassword())) {
//
//
//            this.writeResult(response, ResultCode.UNMOBILEORPASSWORD);
//
//        } else {
//
////            String token = JwtUtil.createJWT(user2.getId(), user2.getUsername(), new HashMap<>());
//            Map<String, Object> map = new HashMap<>();
//            map.put("uid", user2.getId());
//            map.put("userName", user2.getUsername());
//
//            String token = TokenUtil.genAccessToken(map);
//
//            //2. 响应结果
//
//            this.writeResultAndData(response, token);
//
//        }


    }

    /**
     * 获取个人信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void profile(HttpServletRequest request, HttpServletResponse response) {

//
//        //请求中获取key为Authorization的头信息
//        String authorization = request.getHeader("Authorization");
//        if (StringUtils.isNullOrEmpty(authorization)) {
//            throw new CommonException(ResultCode.UNAUTHENTICATED);
//
//        }
//        //前后端约定头信息内容以 Bearer+空格+token 形式组成
//        String token = authorization.replace("Bearer ", "");
//
//        if (TokenUtil.isTokenExpired(token)) {
//            throw new CommonException(ResultCode.UNLOGINEXPIRE);
//        }
//
//        //比较并获取claims
//        Claims claims = TokenUtil.parsePayload(token);
//
//
//        if (claims == null) {
//            throw new CommonException(ResultCode.UNAUTHENTICATED);
//        }
//
//
//        String userId = claims.get("uid", String.class);
//
//        User user = userService.findById(userId);
//
//        this.writeResultAndData(response, new ProfileResult(user));

    }

}
